package org.jcms.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jcms.model.Order;
import org.jcms.model.PageBean;
import org.jcms.model.Proof;
import org.jcms.service.OrderServiceI;
import org.jcms.service.ProofServiceI;
import org.jcms.uitils.DateUtil;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 基于分页的订单查看（全部）
 * 
 * @author Administrator
 * 
 */
public class ManagerGetAllOrders extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ManagerGetAllOrders() {
		super();
	}

	public void destroy() {
		super.destroy();

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		OrderServiceI orderServiceI = (OrderServiceI) ac
				.getBean("orderServiceImpl");
		ProofServiceI proofServiceI = (ProofServiceI) ac
				.getBean("proofServiceImpl");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String retJson = "";
		int intPage = 1;
		int intRows = 10;

		JSONObject jsonObject = new JSONObject();

		try {
			intPage = Integer.parseInt(page);
			intRows = Integer.parseInt(rows);
			if (intPage <= 0) {
				intPage = 1;
			}
			if (intRows > 200) {
				intRows = 10;
			}

			List<Order> orders = null;
			PageBean<Order> pbOrder = null;

			String hql = "from Order order by orderTime desc";
			pbOrder = orderServiceI.findByPage(hql, intPage, intRows);

			jsonObject.put("total", pbOrder.getTotalRows());

			orders = pbOrder.getData();
			List<HashMap<String, String>> orderls = new ArrayList<HashMap<String, String>>();

			for (Order order : orders) {
				HashMap<String, String> tempMap = new HashMap<String, String>();
				tempMap.put("orderId", order.getOrderId());
				tempMap.put("customerId", order.getCustomer().getCustomerId());
				tempMap.put("orderSum", order.getOrderSum() + "");
				tempMap.put("orderTime",
						DateUtil.formatDatetime(order.getOrderTime()));
				tempMap.put("stateName", order.getState().getStateName());

				List<Proof> proofls = proofServiceI.findByHQL(
						"from Proof where order.orderId = ?",
						order.getOrderId());
				if (proofls != null && proofls.size() > 0) {
					Proof proof = proofls.get(0);
					tempMap.put("proofId", proof.getProofId());
					tempMap.put("proofStateName", proof.getProofstate().getProofStateName());
				} else {
					tempMap.put("proofId", "没有生成领书单");
				}
				orderls.add(tempMap);
			}
			jsonObject.put("rows", orderls);
			retJson = jsonObject.toString();
		}

		catch (Exception e) {
           e.printStackTrace();
		}

		finally {
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	public void init() throws ServletException {

	}

}
